<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Setup extends Base
{
    //关于我们
    public function about()
    {
        $where = ["about_type" => 1];
        $find = db("about")->where($where)->find();
        $this->assign("info", $find);
        return $this->fetch("about");
    }

    //驾驶员申请协议
    public function agreement()
    {
        $where = ["about_type" => 2];
        $find = db("about")->where($where)->find();
        $this->assign("info", $find);
        return $this->fetch("about");
    }

    //更新协议
    public function updset()
    {
        $where = ["about_id" => input("about_id"), "about_body" => input("about_body")];
        $find = db("about")->update($where);
        if (false === $find) $this->ajaxReturn(603, errorMsg);
        $this->ajaxReturn(200, successMsg);
    }

    public function phone()
    {
        if (request()->isGet()) {
            $find = db("contact")->find();
            $this->assign("info", $find);
            return $this->fetch("phone");
        }
        $data = input("post.");
        $data["contact_id"] = 1;
        $update = db("contact")->update($data);
        if (false === $update) $this->ajaxReturn(603, errorMsg);
        $this->ajaxReturn(200, successMsg);

    }

    //后台设置信息
    public function setInfo()
    {
        $admin_id = session('admin_id');
        if (request()->isGet()) {
            $info = db('admin_info')->where('admin_id', $admin_id)->find();
            $this->assign('info', $info);
            return $this->fetch('setoinfo');
        }
        $data = input();
        if (!empty($data["img"])) {
            $oldPath = config("TEMPORARY_CATALOG_FILE_PATH") . getFileName($data["img"]);
            $newPath = config("ADMININFO_FILE_PATH");
            $path = moveFile($oldPath, $newPath);
            if (!$path) $this->ajaxReturn(603, selectFile);
            $data["admin_info_logo"] = $path;
            unset($data['img']);
        }
        unset($data['file']);
        $is_store = session('is_store');
        if ($is_store == 1) {
            $store = [
                'store_name' => $data['admin_info_name'],
                'store_address' => $data['admin_info_address'],
                'store_text' => $data['admin_info_text'],
                'store_phone' => $data['admin_info_phone'],
            ];
            if ($data["admin_info_logo"]) {
                $store['store_logo'] = $data["admin_info_logo"];
            }
            $res = db('store')->where('admin_id', $data['admin_id'])->update($store);
            if (!$res) $this->ajaxReturn(603, '网络错误~');
        }
        $res = db('admin_info')->update($data);
        if ($res) $this->ajaxReturn(200, '操作成功');
        else $this->ajaxReturn(603, '操作失败');
    }

}